package com.donleo.security.controller;

import com.donleo.security.common.CommonResult;
import com.donleo.security.service.UserService;
import com.donleo.security.vo.LoginParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author liangd
 * @since 2021-01-16 9:34
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginParams loginParams) {
        HashMap<String, String> data = new HashMap<>();
        String token = null;
        try {
            token = userService.login(loginParams);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.validateFailed("用户名或密码错误");
        }
        if (StringUtils.isEmpty(token)) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        data.put("tokenHead", tokenHead);
        data.put("token", token);
        data.put("code", loginParams.getCode());
        return CommonResult.success(data);
    }

    /**
     * 判断是否有访问路径的权限
     */
    @PostMapping("/checkAuth")
    public CommonResult checkAuth(@RequestParam String userCode,@RequestParam String uri){
        return userService.checkAuth(userCode,uri);
    }
}
